import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class Test6 {
    public static void rotate1(int[] nums, int k) {
        if(nums == null){
            return;
        }
        int len = nums.length;
        int[] array = new int[len];
        for(int i=0;i<len;i++){
            array[(i+k)% len] = nums[i];
        }
        System.arraycopy(array,0,nums,0,len);//写回元素组nums
    }
    public static void rotate(int[] nums, int k) {
        if(nums == null ||k==0){
            return;
        }
        k = k % nums.length; //处理 k > nums.length 的情况
        reserve(nums,0, nums.length -1);//反转整个数组
        reserve(nums,0,k-1);//反转前k-1的数组
        reserve(nums,k,nums.length-1);//反转剩余的数组
    }
    public static void reserve(int[] nums,int start,int end){
        //进行交换
        while (start < end){
            int tmp = nums[start];
            nums[start] = nums[end];
            nums[end] = tmp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4,5,6,7};
        int k = 3;
        /*int[] nums = new int[]{-1,-100,3,99};
        int k = 2;*/
        rotate(nums,k);
       System.out.println(Arrays.toString(nums));
    }
}
